package bean;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class YeeLink {
    
    /**
     * Get Request
     * @return
     * @throws Exception
     */
    public static String doGet() throws Exception {
    	String url="http://api.yeelink.net/v1.0/device/69195/sensors";
        URL localURL = new URL(url);
        URLConnection connection = localURL.openConnection();
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
        
        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("U-ApiKey","5f8fdc543f5362b6adbeef6532e68313");
        
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
        
        if (httpURLConnection.getResponseCode() >= 300) {
            throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
        }
        
        try {
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream,"utf-8");
            reader = new BufferedReader(inputStreamReader);
            
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
            
        } finally {
            
            if (reader != null) {
                reader.close();
            }
            
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            
            if (inputStream != null) {
                inputStream.close();
            }
            
        }
        
        return resultBuffer.toString();
    }
    
    public static boolean postState(String sensor,int action){
    	boolean isOk = false;
    	Map<String, Integer> keyMap = new HashMap<String, Integer>(){
    		{
    			put("switch1", 86618);
    			put("switch2", 86619);
    			put("switch3", 86620);
    			put("light", 325512);
    			put("window", 325513);
    			put("curtain", 325514);
    			put("door", 325515);
    			put("airCondition", 325516);
    		}
    	};
    	int senorID = keyMap.get(sensor);
    	String RSTurl = "http://api.yeelink.net/v1.0/device/69195/sensor/"+senorID+"/datapoints";
    	
    	try{
    	// 建立连接   
    	URL url = new URL(RSTurl);   
    	HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();   
    	  
    	   
    	// //设置连接属性   
    	httpConn.setDoOutput(true);// 使用 URL 连接进行输出   
    	httpConn.setDoInput(true);// 使用 URL 连接进行输入   
    	httpConn.setUseCaches(false);// 忽略缓存   
    	httpConn.setRequestMethod("POST");// 设置URL请求方法  
    	httpConn.setRequestProperty("U-ApiKey","5f8fdc543f5362b6adbeef6532e68313");
    	String requestString = "{\"value\":"+action+"}";   
    	  
    	   
    	// 设置请求属性   
    	// 获得数据字节数据，请求数据流的编码，必须和下面服务器端处理请求流的编码一致   
    	byte[] requestStringBytes = requestString.getBytes("utf-8");   
    	httpConn.setRequestProperty("Content-length", "" + requestStringBytes.length);   
    	httpConn.setRequestProperty("Content-Type", "application/octet-stream");   
    	httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接   
    	httpConn.setRequestProperty("Charset", "UTF-8");   
    	  
    	   
    	// 建立输出流，并写入数据   
    	OutputStream outputStream = httpConn.getOutputStream();   
    	outputStream.write(requestStringBytes);   
    	outputStream.close();   
    	// 获得响应状态   
    	int responseCode = httpConn.getResponseCode();   
    	  
    	   
    	if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功   
    		isOk = true;
    	}   
    	} catch (Exception ex) {   
    	ex.printStackTrace();   
    	}   
    	
    	return isOk;
    }
    
}
